#include <reg52.h>
#include "tube.h"

uchar num_code[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xBF};// ?????0~9????(?????????)

//-----------------------------------------------------------------
//               IO ????
//-----------------------------------------------------------------
//    3641BH         |     51         
//     Pin1         -->   P0^4				
//     Pin2         -->   P0^3			
//     Pin3         -->   P0^7			
//     Pin4         -->   P0^2
//     Pin5         -->   P0^6
//     Pin6         -->   P2^0
//     Pin7         -->   P0^1
//     Pin8         -->   P2^1
//     Pin9         -->   P2^2
//     Pin10        -->   P0^5
//     Pin11        -->   P0^0
//     Pin12        -->   P2^3
//-----------------------------------------------------------------

void delay(unsigned int k)
{
	while(k--);
}

void tube_display(uchar left_end, uchar middle_left, uchar middle_right, uchar right_end, uchar digit){
	P0 = 0xff; // ?P0???????
	if(digit<1) return;
        P2 = 0x01; // ????????
	if(digit>=4) P0 = num_code[right_end]; // ????????????1
	else P0 = 0xff;
        delay(500); // ??
        
	P0 = 0xff; // ?P0???????
        P2 = 0x02; // ????????
	if(digit>=3) P0 = num_code[middle_right]; // ????????????2
	else P0 = 0xff;
        delay(500); // ??

	P0 = 0xff; // ?P0???????
        P2 = 0x04; // ????????
	if(digit>=2) P0 = num_code[middle_left]; // ????????????3
	else P0 = 0xff;
        delay(500); // ??

	P0 = 0xff; // ?P0???????
        P2 = 0x08; // ????????
	if(digit>=1) P0 = num_code[left_end]; // ????????????4
	else P0 = 0xff;
        delay(500); // ??
}
